﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using System.Data;
namespace Adams.Admin.DataAccess.JummahPrayers
{
    public static class Index
    {
        public static Adams.Admin.DataTransfer.JummahPrayers.Index.PageLoad PageLoad()
        {
            var data = new Adams.Admin.DataTransfer.JummahPrayers.Index.PageLoad();
            

            using (DataSet ds = DB.RunDataSet(@"
SELECT * FROM JummahPrayers.Locations
SELECT * FROM JummahPrayers.Prayers
", CommandType.Text))
            {
                ds.Relations.Add("Prayers", ds.Tables[0].Columns["LocationID"], ds.Tables[1].Columns["LocationID"]);

                data.Locations = new Shared.Entities.JummahPrayers.LocationCollection();
                foreach (DataRow dr in ds.Tables[0].Rows)
                {
                    var location = new Shared.Entities.JummahPrayers.Location();
                    location.ID = dr["LocationID"].DbInt();
                    location.Name = dr["Name"].DbString();
                    location.Street = dr["Street"].DbString();
                    location.Street2 = dr["Street2"].DbString();
                    location.City = dr["City"].DbString();
                    location.State = dr["State"].DbString();
                    location.ZipCode = dr["ZipCode"].DbString();
                    location.FridayNote = dr["FridayNote"].DbString();
                    location.LongNote = dr["LongNote"].DbString();
                    location.Latitude = dr["Latitude"].DbDecimal();
                    location.Longitude = dr["Longitude"].DbDecimal();
                    location.DisplayOrder = dr["DisplayOrder"].DbInt();
                    data.Locations.Add(location);

                    location.Prayers = new Shared.Entities.JummahPrayers.PrayerCollection();
                    foreach (DataRow dr2 in dr.GetChildRows("Prayers"))
                    {
                        var prayer = new Shared.Entities.JummahPrayers.Prayer();
                        prayer.ID = dr2["PrayerID"].DbInt();
                        prayer.Name = dr2["Name"].DbString();
                        prayer.Time = dr2["Time"].DbString();
                        prayer.DisplayOrder = dr2["DisplayOrder"].DbInt();
                        location.Prayers.Add(prayer);
                    }
                }
            }

            return data;
        }

        public static void DeleteLocation(int id)
        {
            DB.Run(@"
DELETE JummahPrayers.Prayers
WHERE LocationID = @LocationID

DELETE JummahPrayers.Locations
WHERE LocationID = @LocationID
", CommandType.Text,
                    DB.CreateParameter("@LocationID", id, SqlDbType.Int));
        }

        public static void DeletePrayer(int id)
        {
            DB.Run(@"
DELETE JummahPrayers.Prayers
WHERE PrayerID = @PrayerID
", CommandType.Text,
                    DB.CreateParameter("@PrayerID", id, SqlDbType.Int));
        }
    }
}
